<template>
  <view>
    <pageModule
      hasPrivacy
      pageTitle="新用户注册"
      @submit="submit"
      :disabled="!form.phone"
    >
      <template #header>
        <pageInp
          label="手机号"
          placeholder="请输入手机号"
          inputType="tel"
          :value.sync="form.phone"
        ></pageInp>
        <pageInp
          v-if="stores.is_password_login_phone_verify"
          label="验证码"
          isGetCode
          placeholder="请输入验证码"
          :value.sync="form.verify_code"
          :phone="form.phone"
          code_type="login"
        ></pageInp>
        <pageInp
          label="账户密码"
          placeholder="请输入密码"
          inputType="password"
          :value.sync="form.password"
        ></pageInp>
        <pageInp
          label="确认密码"
          placeholder="请确认密码"
          inputType="password"
          :value.sync="form.re_password"
        ></pageInp>
      </template>
    </pageModule>

    <pagePrivacy ref="pagePrivacy"></pagePrivacy>
  </view>
</template>

<script>
import pageModule from "../components/page-module";
import pageInp from "../components/page-inp";
import pagePrivacy from "../components/page-privacy.vue";
import { regMobile } from "@/common/regExp";

export default {
  components: {
    pageModule,
    pageInp,
    pagePrivacy
  },

  data() {
    return {
      form: {
        phone: "",
        verify_code: "",
        password: "",
        re_password: ""
      }
    };
  },
  methods: {
    async submit() {
      let { form } = this;
      let share_user_id = uni.getStorageSync("parent_user_id") || "";
      if (!form.phone) return this.$u.toast("请输入手机号");
      if (!regMobile(form.phone))
        return this.$utils.toast("请填写正确的手机号~");
      if (this.stores.is_password_login_phone_verify && !form.verify_code)
        return this.$u.toast("请输入验证码");
      if (!form.password) return this.$u.toast("请输入密码");
      if (!form.re_password) return this.$u.toast("请输入确认密码");
      if (form.password !== form.re_password)
        return this.$u.toast("两次密码输入不一致");

      if (!this.$refs.pagePrivacy.hasChecked()) return;

      try {
        const res = await this.$allrequest.user.userPasswordRegedit(
          { ...form, share_user_id },
          true
        );

        if (!res.code) {
          this.$utils.showModal("提示", "注册成功", false, () => {
            this.$utils.goBack();
          });
        }
      } catch (error) {
        this.$u.toast(error);
      }
    }
  }
};
</script>
